const router = require("koa-router")();
const MembersModel = require("../models/members")
// 查询数据库
const fileUpload = require("../utils/upload")

router.post('/doEdit',async ctx=> {
    // console.log(ctx.req.body)
    // console.log(ctx.req.file.filename)
    var file = ctx.request.files.file;
    var uploadName = path.basename(file.path);
    var isUpload = Boolean(file.name.trim());
    var {id,name,age,like,friend} = ctx.request.body;
    var data = {
        name,
        age: Number(age),
        like: Boolean(like),
        friend: Boolean(friend),
        avatar
    }
    if(isUpload){
        fileUpload(file.path,uploadName);
        var avatar = ctx.origin +"/"+uploadName;
        data.avatar = avatar
    }
    await MembersModel.updateOne({_id:id},data)
    ctx.redirect("/")
}) 
module.exports = router;